<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
      :address-info="info"
    />
  </div>
</template>

<script>
import Vue, { set } from "vue";
import { Toast } from 'vant';
import { AddressEdit } from "vant";
import { Area } from 'vant';
import { areaList } from '@vant/area-data';
import {mapMutations} from "vuex"
Vue.use(Area);
Vue.use(AddressEdit);
export default {
     data() {
    return {
      areaList,
      searchResult: [],
      info:{},
      index: -1 // 如有有 this.$route.query.index，index得值为this.$route.query.index，
       // 如果没有，index的值为-1
    };
  },
  mounted(){
      console.log(this.$route.query);
       // 不等于-1 说明this.$route.query传递过来值了，说明执行编辑逻辑，如果等于-1 执行新增逻辑
        
                // this.info = JSON.parse(this.$route.query.item)
            //  this.info =  JSON.parse(this.$route.query.item!=='undefined'?this.$route.query.item:'[]')||[]
                 console.log(Object.keys(this.$route.query),2222);
                 if (Object.keys(this.$route.query).length ) {
                   // 将对象的键转化为一个数组，如果数组的长度为0，就说明这个对象是一个空对象，做一个对象得非空判断
                   // JSON.parse ， JSON.stringfy 后的值不能是一个udifined
                    this.info = JSON.parse(this.$route.query.item)
                 } 
          // 将传递过来的字符串转化为对象
          this.index =  this.$route.query.index || -1  // index就用传递过来的字符串就可以了
       
     
     
       
  },
  methods: {
    ...mapMutations(["address","editaDDRESS","deleleitem"]),
    onSave(content) {
         
        console.log(this.index,111111)
        console.log(content)
        let address = content.province + content.city + content.county + content.addressDetail;
        content.address = address;
        if (this.index == -1) {
              this.address(content)
        } else {
             console.log(this.index,2222222);
            this.editaDDRESS(Object.assign({},{index:this.index},content))
        }
        
        
          this.$router.push("/address")

    },
    onDelete() {
        this.deleleitem(this.index)
         this.$router.push("/address")
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>

<style>
</style>